package com.yidu.BusinessParament.service.impl;


import com.google.gson.Gson;
import com.yidu.BusinessParament.entity.ExchangeRate;
import com.yidu.BusinessParament.mapper.ExchangeRateMapper;
import com.yidu.BusinessParament.service.ExchangeRateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
/**
 * @author 苏彬
 * @date 2022/12/9 13:44
 **/
@Service
public class ExchangeRateImpl implements ExchangeRateService {
    @Autowired
    private ExchangeRateMapper mapper;


    /**
     * 分页+模糊查
     *
     * @param page             当前页
     * @param row              每行显示数
     * @param exchangeRateName 交易所
     * @param exchangeType     交易品种类型
     * @return Map layui需要的map
     */
    @Override
    public Map selectExchangeRateLimit(int page, int row, int exchangeRateName, int exchangeType) {
        //创建String接收拼接条件
        String term = append(exchangeRateName, exchangeType).toString();

        //创建空map
        Map selectMap = new HashMap();
        //传入page
        selectMap.put("page", (page - 1) * row);
        //传入row
        selectMap.put("row", row);
        //传入term
        selectMap.put("term", term);

        //创建layUiMap
        Map layUiMap = new HashMap();
        //传入data
        layUiMap.put("data", mapper.selectExchangeRate(selectMap));
        //传入msg
        layUiMap.put("msg", "");
        //传入code
        layUiMap.put("code", 0);
        //传入count
        layUiMap.put("count", mapper.selectExchangeRateCount(selectMap));

        //返回layUiMap
        return layUiMap;
    }

    /**
     * 单删+批量删
     *
     * @param myArr 要删除的id
     */
    @Override
    public void deleteExchangeRate(String myArr) {
        String[] array = new Gson().fromJson(myArr, String[].class);
        for (String Id : array) {
            mapper.deleteExchangeRate(Id);
        }
    }

    /**
     * 增加
     *
     * @param rate 要增加的对象
     */
    @Override
    public int addExchangeRate(ExchangeRate rate) {
        String i = mapper.MaxId();
        if (i == null) {
            i = "0";
        }
        int sum = Integer.parseInt(i);
        rate.setExchangeRateId(sum + 1 + "");
        return mapper.addExchangeRate(rate);
    }

    /**
     * 修改
     *
     * @param rate 修改对象
     */
    @Override
    public String updateExchangeRate(ExchangeRate rate) {
        //创建String msg
        String msg = "";
        try {
            //品种名字和 品种类型>0
            if (rate.getExchangeRateName() > 0 && rate.getExchangeType() > 0) {
                //执行修改msg 为 ojbk 否nu'l'l
                mapper.updateExchangeRate(rate);
                msg = "ok";
            } else {
                msg = "null";
            }
        } catch (Exception e) {
            //则 error
            msg = "error";
        }
        return msg;
    }


    /**
     * 拼接条件
     *
     * @param exchangeRateName 交易所
     * @param exchangeType     交易品种所
     * @return StringBuffer
     */
    public StringBuffer append(int exchangeRateName, int exchangeType) {
        //创建StringBuffer
        StringBuffer term = new StringBuffer();
        //exchangeRateName>-1 拼接
        if (exchangeRateName > -1) {
            term.append(" and exchangeRateName=").append(exchangeRateName);
        }
        //exchangeType>1 拼接
        if (exchangeType > -1) {
            term.append(" and exchangeType=").append(exchangeType);
        }
        //返回term
        return term;
    }
}
